package com.groupbaby.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.groupbaby.ydd.common.BusinessException;
import com.groupbaby.ydd.common.ResultData;
import com.groupbaby.ydd.dto.SysMenuDto;
import com.groupbaby.ydd.entity.SysMenu;

import com.groupbaby.ydd.service.ISysMenuService;
import com.groupbaby.ydd.service.ISysRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;



    /**
     * 添加菜单
     **/

    @PostMapping("/add")
    public ResultData add(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return ResultData.ok("添加成功", "");

    }


    /**
     * 根据id删除
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {
        sysMenuService.removeById(id);
        return ResultData.ok("删除成功", "");
    }


    /**
     * 修改功能
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenuService.updateById(sysMenu);
        return ResultData.ok("修改成功","");
    }


    /**
     * 菜单列表展示
     **/
    @GetMapping("/allData")
    public ResultData getAllDta() {

        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        //只要一级菜单
        wrapper.eq(SysMenu::getPid, 0);
        List<SysMenu> sysMenus = sysMenuService.list(wrapper);
        System.out.println("sysMenus = " + sysMenus);

        //递归获取子菜单
        for (int i = 0; i < sysMenus.size(); i++) {
            SysMenu s = sysMenuService.getChildren(sysMenus.get(i));
            sysMenus.set(i, s);
        }

        System.out.println("sysMenus = " + sysMenus);
        return ResultData.ok("成功",sysMenus);

    }


    /**
     * 修改时数据回显
     *
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getRowInfo(SysMenu sysMenu) {

        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(sysMenu.getId() != null, SysMenu::getId, sysMenu.getId());
        SysMenu sysMenuInfo = sysMenuService.getOne(wrapper);
        return ResultData.ok("",sysMenuInfo);

    }
}
